package com.qfedu.fmmall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qfedu.fmmall.entity.*;
import com.qfedu.fmmall.mapper.*;
import com.qfedu.fmmall.service.ProductCommentsService;
import com.qfedu.fmmall.service.UsersService;
import com.qfedu.fmmall.utils.PageHelper;
import com.qfedu.fmmall.vo.ResultVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 商品评价  服务实现类
 * </p>
 *
 * @author itcast
 */
@Slf4j
@Service
public class ProductCommentsServiceImpl extends ServiceImpl<ProductCommentsMapper, ProductComments> implements ProductCommentsService {
 @Resource
  private ProductCommentsMapper productCommentsMapper;
 @Resource
 private UsersMapper usersMapper;
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Resource
    private ProductMapper productMapper;
    @Resource
    private ProductSkuMapper productSkuMapper;
    /**
     * 商品评论信息查询接⼝
     * @param pid
     * @return
     */
    @Override
    public ResultVO listCommontsByProductId(String pid,int pageNum,int limit) {
         Page<ProductComments> page = new Page<>(pageNum, limit);
         LambdaQueryWrapper<ProductComments> lambdaQueryWrapper = new LambdaQueryWrapper();
         lambdaQueryWrapper.eq(ProductComments::getProductId, pid);
         productCommentsMapper.selectPage(page, lambdaQueryWrapper);
          if (page.getRecords().size()==0) {
             return ResultVO.error("没有该信息");
         }
         PageHelper<ProductCommentsVO>p=new PageHelper<>();
         p.setList(new ArrayList<ProductCommentsVO>());
         p.setPageCount(page.getSize());
         p.setCount(page.getTotal());
         int i=page.getRecords().size()-1;
           while (i>=0){
             Users users = usersMapper.selectOne(new LambdaQueryWrapper<Users>().eq(Users::getUserId,
                     page.getRecords().get(i).getUserId()));
             ProductCommentsVO vo=new ProductCommentsVO();
             BeanUtils.copyProperties( page.getRecords().get(i), vo);
             vo.setUsername(users.getUsername());
             vo.setUserImg(users.getUserImg());
             vo.setNickname(users.getNickname());
             p.getList().add(vo);
             i--;
         }
        return ResultVO.success(p,"200");
    }
    /**
     * 查询商品评价统计接口
     * @param id
     * @return
     */
    @Override
    public ResultVO commontsCountMap(String id) {
        List<ProductComments> list = productCommentsMapper.selectList(new LambdaQueryWrapper<ProductComments>().eq(
                ProductComments::getProductId, id
        ));
        Map<String ,Object> map=new HashMap<>();
        int goodTotal=0;
        int midTotal=0;
        int badTotal=0;
        if (list.size() == 0){
            return ResultVO.error("找不到该信息" );
        }
        int total=list.size();
        for (ProductComments item: list) {
            if ( item.getCommType().equals(1)){
                  goodTotal++;
            }else if (item.getCommType().equals(-1)){
                midTotal++;
            }else {
                badTotal++;
            }
        }
        DecimalFormat df = new DecimalFormat("#.00");
        double percent=Double.parseDouble(goodTotal+"")/Double.parseDouble(total+"")*100;
       percent= Double.parseDouble(df.format(percent));
        map.put("total",total);
        map.put("goodTotal",goodTotal);
        map.put("midTotal",midTotal);
        map.put("badTotal",badTotal);
        map.put("percent",percent);
        return ResultVO.success(map,"200");
    }
}
